<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>菜单</title>
        <script src="js/jquery.1.10.2.min.js" type="text/javascript"></script>
        <script src="js/nwHelper.js" type="text/javascript"></script>
    </head>
    <style type="text/css">

        *{
            -webkit-user-select: none;//禁止选中文字
            list-style: none;
            outline: none;
            margin: 0;
            padding: 0;
        }

        ::selection {
            background: not specified;
        }
        body,html{
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        #boxMenue{
            width: 450px;
            margin: 0 auto;
            margin-top:30px;
            position: relative;
            background-color: #293840;
            height: 150px;
        }

        #boxMenue ul{
            position: relative;
            display: block;
            width: 100%;
            height: 100%;
        }

        #boxMenue ul li{
            cursor: pointer;
            position: relative;
            border-style: none;
            display: block;
            height: 150px;
            width: 90px;
            float: left;
            z-index: 2;
            line-height: 4.4;
            text-align: center;
            color: white;
            font-size: 35px;            
            /*background-image: url("./image/burger-large.png");*/
            /*background-position: 50%;*/
        }

        #boxMenue ul li:first-child{
            margin-left: 0px;
        }


        #boxMenue #floatBg{
            background-color: #F44336;
            width: 100px;
            position: absolute;
            top: -15px;
            left: -5px;
            height: 180px;
            display: block;
            border-radius: 4px;
            transition-property: left;
            transition-duration: 0.3s;
            transition-timing-function: ease;        
        }
        #boxMenue #floatBg::before,#boxMenue #floatBg::after{
            height: 15px;
            content: "";
            width:100%;
            display: block;
            background: #94221A;
            position: absolute;
        }
        #boxMenue #floatBg::after{
            bottom: 0;
        }        
        #boxMenue #floatBg::before{
            top: 0;
        }

    </style>
    <body>
        <div id="boxMenue">
            <ul>
                <li idCount="0" id="shortMessage">便签</li>
                <li idCount="1" id="schedule">计划</li>
                <li idCount="2" id="config">设置</li>
                <li idCount="3" id="about">关于</li>
                <li idCount="4" id="exit">退出</li>
            </ul>
            <div id='floatBg' class="moveLeve">
            </div>

        </div>
    </body>
    <script>
    // windows 事件
        var gui=require("nw.gui");
        var allwindows=process.mainModule.exports.allWindows;
        win=gui.Window.get();
        fs=require("fs");

        // drag(win,"#boxMenue");
        process.mainModule.exports.mainWindow=win;
        win.on("close",function(){
            if(allwindows.length!=0){
                for(var i=0;i<allwindows.length;i++){
                    if(allwindows[i].win!=null){
                        allwindows[i].win.emit("main-close","222");                        
                    }
                    else{
                        console.log("window is null");
                    }
                }
            }
           this.close(true);
        });
        nwAppInit();
    //系统托盘事件

    var tray;
    // Show tray
    tray = new gui.Tray({
        icon: './image/favicon.png'
    });
    var trayCount=0;
    // Show window and remove tray when clicked
    tray.on('click', function() {
        if(trayCount%2==1){
            win.show();      
        }
        else{
            win.hide();
        }
        trayCount++;
        win.setPosition("center");
    });    

    // Get the minimize event
    win.on('minimize', function() {
        // Hide window
        this.hide();
    });



// 网页内事件

    //当前li的位置
    var liPos=0;
    $("#boxMenue ul li").on("mouseover",function(){
        var numLi=$(this).attr("idCount");
        $("#boxMenue #floatBg").css("transition-property","left");
        $("#boxMenue #floatBg").css("left",90*parseInt(numLi)-5+"px");
        liPos=parseInt(numLi);
    });

// 绑定tab键
    document.onkeydown = keyDownH;
    function keyDownH(e) {
    // 兼容FF和IE和Opera    
        var theEvent = e || window.event;
        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;

        if (code == 9) {
            if(liPos==4){
                liPos=0;
                $("#boxMenue #floatBg").css("transition-property","width");
            }
            else{
                liPos=(++liPos)%5;
                $("#boxMenue #floatBg").css("transition-property","left");
            }
            $("#boxMenue #floatBg").css("left",90*parseInt(liPos)-5+"px");
            return false;
        }
        else if(code==13){
            // alert("模拟点击");
            $("#boxMenue ul li[idCount="+liPos+"]").trigger("click");

            return false;
        }
        return true;
    };


//退出事件
$("#exit").on("click",function(){
    win.close();
})

//便签事件
$("#shortMessage").on("click",function(){
    // 便签为初始化时
    if(process.mainModule.exports.smInintState==0){
        console.log("smInintState");
        fs.exists(process.mainModule.exports.nwPath+"/config/shortMessageData.json", function(exists){
            if(exists){
                fs.readFile(process.mainModule.exports.nwPath+"/config/shortMessageData.json","utf-8",function(err,data){
                    if(!err){
                        var dataJSON=JSON.parse(data);
                        if(dataJSON.length!=0){
                            console.log("json长度："+dataJSON.length);
                            // 初始化便签 回显上次软件关闭时的便签
                            console.log("便签初始化");
                            for(var i=0;i<dataJSON.length;i++){
                                var win2 = gui.Window.open("./shortMessage.html", {
                                    "width": 150,
                                    "height": 400,
                                    "transparent": true,
                                    "frame": false,
                                    "toolbar": false,
                                    "show_in_taskbar":false
                                }); 
                                process.mainModule.exports.smInintState=1;      
                                console.log("窗口压入");                          
                                process.mainModule.exports.allWindows.push({"win":win2,"news":dataJSON[i]});                                             
                            }
                            console.log("便签初始化打开窗口完成");
                            console.log("重写jsonw文件");
                            fs.writeFile(process.mainModule.exports.nwPath+"/config/shortMessageData.json", "[]", {
                                encoding: "UTF-8"
                            }, function(err) {
                                if (err)
                                    alert("内容保存失败！！");

                            });
                        }
                        else{
                            console.log("json文件为空");
                            var win2 = gui.Window.open("./shortMessage.html",{
                                "width": 150,
                                "height": 400,
                                "transparent": true,
                                "frame": false,
                                "toolbar": false,
                                "show_in_taskbar": false
                            });
                            process.mainModule.exports.smInintState=1;

                        }
                        console.log("process.mainModule.exports.smInintState:"+process.mainModule.exports.smInintState);

                    }
                    else{
                        console.log(err);
                    }
                });
            }
            else{
                console.log("文件不存在");
            }
        });
    }
    else{
        win2 = gui.Window.open("./shortMessage.html", {
            "width": 150,
            "height": 400,
            "transparent": true,
            "frame": false,
            "toolbar": false,
            "show_in_taskbar":false
        });                         
    }

    console.log("窗口长度:"+allwindows.length);
   
    win.minimize();
});



    </script>
</html>